package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dto.VersionDTO;
import com.qumitech.visualnovel.service.version.VersionCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: yilu
 * @Date: 2019/1/15 13:08
 * @Description:
 */

@RestController
@RequestMapping("/api/version")
public class VersionCfgController {

    @Autowired
    private VersionCfgService versionCfgService;

    @PostMapping
    public BaseRsp AddVersion(@RequestBody VersionDTO versionDTO){
        versionCfgService.addVersion(versionDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping
    public BaseRsp updateVersion(@RequestBody VersionDTO versionDTO){
        versionCfgService.updateVersion(versionDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping()
    public BaseRsp getVersions(@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                               @RequestParam(value = "offset", defaultValue = "1", required = false) Integer offset,
                               @RequestParam(value = "phone_type", required = false) Byte phoneType){

        return new BaseRsp(ResultEnum.SUCCESS, versionCfgService.getVersion(limit, offset, phoneType));
    }

    @DeleteMapping()
    public BaseRsp deleteVersions(@RequestParam("version") String version,
                                  @RequestParam("phone_type") Byte phoneType,
                                  @RequestParam("app_package") String appPackage){
        boolean isNullParam = version == null || phoneType == null;
        if(isNullParam){
            throw new BusinessException(ResultEnum.PARA_ERR);
        }
        VersionDTO dto = new VersionDTO(phoneType, version, appPackage);
        versionCfgService.deleteVersion(dto);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
}
